package com.foreveross.pdfplugin.plugin;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.artifex.mupdfdemo.MuPDFActivity;

import android.content.Intent;
import android.net.Uri;

/**
 * <BR>
 * [功能详细描述] pdf插件
 * 
 */
public class PDFPlugin extends CordovaPlugin {

	@Override
	public boolean execute(String action, JSONArray args,
			final CallbackContext callbackContext) throws JSONException {
		if (action.equals("renderPDF")) {
			JSONObject jb = args.getJSONObject(0);
			String filePath = jb.getString("file");
			if (filePath == null){
				callbackContext.error("args 参数为空");
			} else {
				openLocalPdf(filePath);
				callbackContext.error("打开文件成功");
			}
		}
		return true;
	}

	private void openLocalPdf(String path) {
		// String pdfFilePath =
		// Environment.getExternalStorageDirectory().getPath() + "/"
		// + URL.APP_PACKAGENAME + "/" + path;
		// Uri uri = Uri.parse(pdfFilePath);
		Uri uri = Uri.parse(path);
		Intent intent = new Intent(cordova.getActivity(), MuPDFActivity.class);
		intent.setAction(Intent.ACTION_VIEW);
		intent.setData(uri);
		cordova.getActivity().startActivity(intent);
	}

	// private void openWebPdf(String path){
	// Intent intent=new Intent();
	// intent.setClass(cordova.getActivity(), WebViewPDFActivity.class);
	// intent.putExtra("path", path);
	// cordova.getActivity().startActivity(intent);
	// }
}